package com.qf.hospital.controller;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageInfo;
import com.qf.hospital.dto.CashierDTO;
import com.qf.hospital.dto.CashierDTO2;
import com.qf.hospital.dto.ReportDTO;
import com.qf.hospital.entity.*;
import com.qf.hospital.enums.HospitalErrorEnums;
import com.qf.hospital.service.*;
import com.qf.hospital.vo.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * @version V1.0
 * @Project hospital
 * @Package com.qf.hospital.controller
 * @Description: ${TODO}
 * @Date 2021/10/20 10:56
 */

@RestController
public class CaoController {

    private Logger log = LoggerFactory.getLogger(CaoController.class);

    @Autowired
    private ReportService reportService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private RegisterTypeService registerTypeService;

    @Autowired
    private DoctorService doctorService;

    @Autowired
    private PharmacyService pharmacyService;

    @Autowired
    private CashierService cashierService;

    @Autowired
    private OutPatientTypeService outPatientTypeService;

    /**
     * 查询患者信息
     *
     * @param cc
     * @param name
     * @return
     */
    @GetMapping("/cao/index")
    public List<ReportVO> index(@RequestParam(defaultValue = "1") Integer cc, String name) {
        //调用service查询数据
        List<ReportVO> list = reportService.findPatientInfo(cc, name);
        //响应
        return list;
    }

    /**
     * 查询科室类型
     *
     * @return
     */
    @PostMapping("/cao/seldep")
    public List<DepartmentVO> seldep() {
        return departmentService.findDepartment();
    }

    /**
     * 查询挂号类型
     *
     * @return
     */
    @PostMapping("/cao/selreg")
    public List<RegisterTypeVO> selreg() {
        return registerTypeService.findRegisterType();
    }

    @PostMapping("/cao/seldoctor")
    public List<DoctorVO> seldoctor(Integer registeredId, Integer departmentId) {
        if (registeredId == null || departmentId == null) {
            log.info("【查询医生信息】 请求参数不合法！ registeredId = {},departmentId = {}", registeredId, departmentId);
            return null;
        }
        List<DoctorVO> list = doctorService.findDoctorByRegisterIdAndDepartmentId(registeredId, departmentId);
        return list;
    }

    /**
     * 查询挂号费用
     *
     * @param registeredId
     * @return
     */
    @PostMapping("/cao/selRegMoney")
    public Integer selRegMoney(Integer registeredId) {
        //1.校验参数
        if (registeredId == null) {
            log.info("【查询挂号费用】 参数不合法！ registeredId = {}", registeredId);
            return -1;
        }
        //2.调用service
        return registerTypeService.findRegMoneyByRegisterId(registeredId);
    }


    /**
     * 通过电话和身份证号查重
     */
    @PostMapping("/cao/phone")
    public Integer checkExist(String phone, String carid) {
        //1.参数校验
        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(carid)) {
            log.info("【校验手机号&身份证号】 手机或者身份证信息为空！】");
            return 0;
        }
        //2.调用service
        return reportService.findPatientByPhoneAndCarid(phone, carid);
    }

    /**
     * 添加患者
     */
    @PostMapping("/cao/addReport")
    @Transactional
    public R addReport(@Valid ReportDTO reportDTO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String msg = bindingResult.getFieldError().getDefaultMessage();
            log.info("【添加患者】 参数不合法！", msg);
            return R.error(HospitalErrorEnums.REQUEST_PARAMETER_ERROR.getCode(), msg);
        }

        //封装录入人信息
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        if (user == null) {
            log.info("【添加患者】 用户登录超时");
            return R.error(HospitalErrorEnums.LOGIN_TIMEOUT);
        }

        reportDTO.setUsers(user.getRealName());

//        Report report = new Report();
//        BeanUtils.copyProperties(reportDTO, report);
//        report.setUsers(user.getRealName());

        reportService.addReport(reportDTO);
        return R.ok("添加成功！");
    }

    /**
     *
     */
    @GetMapping("/caotake/tselpreson")
    public R tselpreson(String name,
                        @RequestParam(defaultValue = "1") Integer page,
                        @RequestParam(defaultValue = "10") Integer limit) {

        PageInfo<Report> pageInfo = reportService.findPresentionInfo(name, page, limit);

        long count = pageInfo.getTotal();
        List<ReportVO> data = new ArrayList<>();
        for (Report report : pageInfo.getList()) {
            ReportVO reportVO = new ReportVO();
            BeanUtils.copyProperties(report, reportVO);
            data.add(reportVO);
        }

        return R.ok(count, data);
    }


    @GetMapping("/caocc/seldrug")
    public R seldrug(String drugname,
                     @RequestParam(defaultValue = "1") Integer page,
                     @RequestParam(defaultValue = "10") Integer limit) {
        PageInfo<Pharmacy> pageInfo = pharmacyService.findPharmacyInfo(drugname, page, limit);
        long count = pageInfo.getTotal();
        List<PharmacyVO> data = new ArrayList<>();
        for (Pharmacy pharmacy : pageInfo.getList()) {
            PharmacyVO vo = new PharmacyVO();
            BeanUtils.copyProperties(pharmacy, vo);
            data.add(vo);
        }
        return R.ok(count, data);
    }


    @GetMapping("/caocc/selAll")
    public R selALL(Integer perid,
                    @RequestParam(defaultValue = "1") Integer page,
                    @RequestParam(defaultValue = "10") Integer limit) {
        PageInfo<Cashier> pageInfo = cashierService.findMedicineByReportId(perid, page, limit);
        long count = pageInfo.getTotal();
        List<CashierVO> data = new ArrayList<>();
        for (Cashier cashier : pageInfo.getList()) {
            CashierVO vo = new CashierVO();
            BeanUtils.copyProperties(cashier, vo);
            data.add(vo);
        }
        return R.ok(count, data);
    }

    @PostMapping("/caocc/selbing")
    public R selbing(Integer reid) {
        String msg = reportService.findSickReason(reid);
        return R.ok(msg);
    }

    @PostMapping("/caocc/addbing")
    public R addbing(Integer reid, String bing) {
        Report report = reportService.findById(reid);
        report.setZhuan(bing);
        return R.ok("病因添加成功！");
    }

    @PostMapping("/caocc/selchu")
    public Integer selchu(Integer reid, String mename) {

        return cashierService.findNotPayMedicineByName(reid, mename);
    }

    @PostMapping("/caocc/addchu")
    public R addchu(@Valid CashierDTO cashierDTO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String msg = bindingResult.getFieldError().getDefaultMessage();
            log.info("【添加处方药】 添加处方药 参数有误！", msg);
            return R.error(HospitalErrorEnums.REQUEST_PARAMETER_ERROR, msg);
        }
        Cashier cashier = new Cashier();
        BeanUtils.copyProperties(cashierDTO, cashier);
        cashier.setState(0);
        cashier.setOstate(0);
        cashier.setMstate(0);
        cashier.setDrugName(cashierDTO.getName());
        cashier.setDrugNum(cashierDTO.getNum());
        cashier.setRepicePrice(cashierDTO.getPrice());
        cashier.setRepiceTotal(cashierDTO.getTotalPrice());
        cashier.setDrugStoreNum(cashierDTO.getDrugstorenum());
        cashier.setReportId(cashierDTO.getReportId());
        cashier.setCashierId(cashierDTO.getRegisterId());
        cashier.setCtime(new Date());
        cashierService.addCashier(cashier);
        return R.ok("添加成功！");
    }

    /**
     * 删除药品
     */
    @PostMapping("/caocc/del")
    public R del(Integer cashierId) {
        cashierService.del(cashierId);
        return R.ok("删除成功！");
    }


    /**
     * 项目划价---找患者
     */
    @GetMapping("/caocc/selpreson")
    public R selpreson(String name,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit) {

        PageInfo<Report> pageInfo = reportService.findPresentionInfo(name, page, limit);

        long count = pageInfo.getTotal();
        List<ReportVO2> data = new ArrayList<>();
        for (Report report : pageInfo.getList()) {
            ReportVO2 reportVO = new ReportVO2();
            BeanUtils.copyProperties(report, reportVO);
            data.add(reportVO);
        }

        return R.ok(count, data);
    }

    @GetMapping("/caoout/selout")
    public R selout(String projectName,
                    @RequestParam(defaultValue = "1") Integer page,
                    @RequestParam(defaultValue = "10") Integer limit) {
        PageInfo<OutPatientType> pageInfo = outPatientTypeService.findOutPatientTypeInfo(projectName, page, limit);
        long count = pageInfo.getTotal();
        List<OutPatientTypeVO> data = new ArrayList<>();
        for (OutPatientType outPatientType : pageInfo.getList()) {
            OutPatientTypeVO vo = new OutPatientTypeVO();
            BeanUtils.copyProperties(outPatientType, vo);
            data.add(vo);
        }
        return R.ok(count, data);
    }

    @GetMapping("/caocc/selximu")
    public R selximu(Integer perid,
                     @RequestParam(defaultValue = "1") Integer page,
                     @RequestParam(defaultValue = "10") Integer limit) {
        PageInfo<Cashier> pageInfo = cashierService.findXiangmuByReportId(perid, page, limit);
        long count = pageInfo.getTotal();
        List<CashierVO2> data = new ArrayList<>();
        for (Cashier cashier : pageInfo.getList()) {
            CashierVO2 vo = new CashierVO2();
            BeanUtils.copyProperties(cashier, vo);
            data.add(vo);
        }
        return R.ok(count, data);
    }

    @PostMapping("/cao/delo")
    public R delo(Integer cashier) {
        cashierService.delo(cashier);
        return R.ok("删除成功！");
    }


//    public Integer selchuo(Integer reid, String durgname) {
//
//        Integer i = cashierService.findNotPayXiangMuByName(reid, durgname);
//
//        return i;
//    }

    @PostMapping("/caoout/addchuo")
    public R addchuo(@Valid CashierDTO2 cashierDTO2, BindingResult bindingResult) {

        if (bindingResult.hasErrors()) {
            String msg = bindingResult.getFieldError().getDefaultMessage();
            log.info("【添加项目】 添加项目 参数有误！", msg);
            return R.error(HospitalErrorEnums.REQUEST_PARAMETER_ERROR, msg);
        }

        Cashier cashier = new Cashier();
        BeanUtils.copyProperties(cashierDTO2, cashier);

        cashierService.addCashier(cashier);
        return R.ok("添加成功！");
    }


    @GetMapping("/caocc/mohu")
    public R mohu(String durgname,
                  @RequestParam(defaultValue = "1") Integer page,
                  @RequestParam(defaultValue = "10") Integer limit) {
        PageInfo<Report> pageInfo = reportService.findPatientByDrugName(durgname, page, limit);

        long count = pageInfo.getTotal();
        List<ReportVO2> data = new ArrayList<>();
        for (Report report : pageInfo.getList()) {
            ReportVO2 reportVO = new ReportVO2();
            BeanUtils.copyProperties(report, reportVO);
            data.add(reportVO);
        }

        return R.ok(count, data);
    }
}
